package com.suny.guarded_suspension.talk;

import com.suny.guarded_suspension.BlockingQueue.RequestQueue;
import com.suny.guarded_suspension.Request;

public class TalkThread extends Thread {
    private final RequestQueue input;
    private final RequestQueue output;

    public TalkThread(String name, RequestQueue input, RequestQueue output) {
        super(name);
        this.input = input;
        this.output = output;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + ": BEGIN");
        for (int i = 0; i < 20; i++) {
            output.putRequest(new Request("test"));
            //接收消息
            Request request = input.getRequest();
            System.out.println(Thread.currentThread().getName() + ": get " + request.getName());

            //重新设置值
            Request request1 = new Request(request.getName() + "!");
            System.out.println(Thread.currentThread().getName() + ": put " + request1.getName());
            output.putRequest(request1);
        }
        System.out.println(Thread.currentThread().getName() + ": END");
    }
}
